package graph;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-05-27
 * Time: 13:43
 */
public class GraphByNode {
    static class Node{
        public int src ; //起始位置
        public int dest ;//目标位置
        public int weight ; //权重
        public Node next;

        public Node(int src, int dest, int weight) {
            this.src = src;
            this.dest = dest;
            this.weight = weight;
        }
    }
    public char[] arrayV ;
    public ArrayList<Node > list ;
    public boolean isDirect;
    public GraphByNode(int size , boolean isDirect){
        arrayV = new char[size];
        list = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            list.add(null);
        }
        this.isDirect = isDirect ;
    }
    public void initArrayV(char[] array){
        for (int i = 0; i < array.length; i++) {
            arrayV[i] = array[i];
        }
    }
    //添加边
    public void addEdge(char src,char dest , int weight){
        int srcIndex = getIndexOfV(src);
        int destIndex = getIndexOfV(dest);
        addEdgeChild(srcIndex,destIndex,weight);
      if(!isDirect){
          addEdgeChild(destIndex,srcIndex,weight);
      }
    }
    public void addEdgeChild(int srcIndex,int destIndex ,int weight){
        Node cur = list.get(srcIndex);
        while(cur !=null){
            if(cur.dest == destIndex){
                return;
            }
            cur = cur.next;
        }
        Node node = new Node(srcIndex,destIndex,weight);
        node.next = list.get(srcIndex);
        list.set(srcIndex,node);
    }
    private int getIndexOfV(char ch){
        for (int i = 0; i < arrayV.length; i++) {
            if(arrayV[i] == ch){
                return i;
            }
        }
        return -1;
    }
    public void printGraph() {
        for (int i = 0; i < arrayV.length; i++) {
            System.out.print(arrayV[i]+"->");
            Node cur = list.get(i);
            while (cur != null) {
                System.out.print(arrayV[cur.dest]+" ->");
                cur = cur.next;
            }
            System.out.println();
        }
    }
    public int getDevOfV(char ch){
        int srcIndex = getIndexOfV(ch);
        Node cur = list.get(srcIndex);
        int count =0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        if( isDirect){
            int destIndex = srcIndex;
            for (int i = 0; i < arrayV.length; i++) {
                int a = getIndexOfV(arrayV[i]);
                Node curr = list.get(a);
               while(curr != null){
                   if(curr.dest == destIndex) {
                       count++;
                   }
                   curr =curr.next;
               }
            }
        }
        return count;
    }
    public static void main(String[] args) {

        GraphByNode graph = new GraphByNode(4,false);
        char[] array = {'A','B','C','D'};
        graph.initArrayV(array);

        graph.addEdge('A','B',1);
        graph.addEdge('A','D',1);
        graph.addEdge('B','A',1);
        graph.addEdge('B','C',1);
        graph.addEdge('C','B',1);
        graph.addEdge('C','D',1);
        graph.addEdge('D','A',1);
        graph.addEdge('D','C',1);

        System.out.println("getDevOfV:: "+graph.getDevOfV('A'));
        graph.printGraph();
    }
}
